<?php

namespace App\Admin\Controllers;

use App\Admin\Filters\TimestampBetween;
use App\Http\Traits\GlobalConstant;
use App\Models\Project;
use App\Models\Team;
use App\Models\TeamUser;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class TeamController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '已解散', 'color' => 'danger'],
        'off' => ['value' => 1, 'text' => '正常', 'color' => 'default'],
    ];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('队伍列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('队伍详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑队伍')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增队伍')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Team());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('user_id', '创建者ID');

                $filter->equal('projectInfo.id', '项目')->select(Project::pluck('name as text','id'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->use(new TimestampBetween('created_at', __('created_at')))->datetime();
            });
        });
//
        $grid->model()->orderBy('created_at', 'desc');
        $grid->column('id', '队伍ID')->sortable()->copyable();
        $grid->column('name', '队伍名称')->sortable();
        $grid->column('discover', '队伍头像')->lightbox(['width' => '50', 'height' => '50']);
        $grid->column('projectInfo.name', '项目')->sortable();
        $grid->column('userInfo.nickname', '创建者')->sortable();
        $grid->column('qty', '成员数')->sortable()->modal('成员详情', function ($model) {
            $arr      = TeamUser::where('team_id',$model->id)->get(['nickname','is_captain']);
            return new Table(['昵称', '队职'], $arr->toArray());
        });
        $grid->column('status', '状态')->switch(self::INVITE_STATUS)->sortable();
        $grid->column('created_at', __('created_at'));
        $grid->disableActions();
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
//
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableBatchActions();
        $grid->expandFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Team::findOrFail($id));
        $show->field('id', '队伍ID');
        $show->field('name', '队伍名称');
        $show->field('projectInfo.name', '项目');
        $show->field('userInfo.nickname', '创建者');
        $show->field('qty', '成员数');
        $show->field('status', '状态');
        $show->field('created_at', __('created_at'));
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Team());


        $form->radio('status', '状态')->options(self::INVITE_STATUS);
        $form->image('email', '图片');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

}
